home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Constructor.java < prev    next >
Text File  |  1998-09-22  |  9KB  |  236 lines

  1. /*
  2.  * @(#)Constructor.java    1.14 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang.reflect;
  16.  
  17. /**
  18.  * Constructor provides information about, and access to, a single
  19.  * constructor for a class.
  20.  *
  21.  * <p>Constructor permits widening conversions to occur when matching the
  22.  * actual parameters to newInstance() with the underlying
  23.  * constructor's formal parameters, but throws an
  24.  * IllegalArgumentException if a narrowing conversion would occur.
  25.  *
  26.  * @see Member
  27.  * @see java.lang.Class
  28.  * @see java.lang.Class#getConstructors()
  29.  * @see java.lang.Class#getConstructor()
  30.  * @see java.lang.Class#getDeclaredConstructors()
  31.  *
  32.  * @author    Nakul Saraiya
  33.  */
  34. public final
  35. class Constructor implements Member {
  36.  
  37.     private Class        clazz;
  38.     private int            slot;
  39.     private Class[]        parameterTypes;
  40.     private Class[]        exceptionTypes;
  41.  
  42.     /**
  43.      * Constructor.  Only the Java Virtual Machine may construct
  44.      * a Constructor.
  45.      */
  46.     private Constructor() {}
  47.  
  48.     /**
  49.      * Returns the Class object representing the class that declares
  50.      * the constructor represented by this Constructor object.
  51.      */
  52.     public Class getDeclaringClass() {
  53.     return clazz;
  54.     }
  55.  
  56.     /**
  57.      * Returns the name of this constructor, as a string.  This is
  58.      * always the same as the name of the constructor's declaring
  59.      * class.
  60.      */
  61.     public String getName() {
  62.     return getDeclaringClass().getName();
  63.     }
  64.  
  65.     /**
  66.      * Returns the Java language modifiers for the constructor
  67.      * represented by this Constructor object, as an integer. The
  68.      * Modifier class should be used to decode the modifiers.
  69.      *
  70.      * @see Modifier
  71.      */
  72.     public native int getModifiers();
  73.  
  74.     /**
  75.      * Returns an array of Class objects that represent the formal
  76.      * parameter types, in declaration order, of the constructor
  77.      * represented by this Constructor object.  Returns an array of
  78.      * length 0 if the underlying constructor takes no parameters.
  79.      */
  80.     public Class[] getParameterTypes() {
  81.     return Method.copy(parameterTypes);
  82.     }
  83.  
  84.     /**
  85.      * Returns an array of Class objects that represent the types of
  86.      * the checked exceptions thrown by the underlying constructor
  87.      * represented by this Constructor object.  Returns an array of
  88.      * length 0 if the constructor throws no checked exceptions.
  89.      */
  90.     public Class[] getExceptionTypes() {
  91.     return Method.copy(exceptionTypes);
  92.     }
  93.  
  94.     /**
  95.      * Compares this Constructor against the specified object.
  96.      * Returns true if the objects are the same.  Two Constructors are
  97.      * the same if they were declared by the same class and have the
  98.      * same formal parameter types.
  99.      */
  100.     public boolean equals(Object obj) {
  101.     if (obj != null && obj instanceof Constructor) {
  102.         Constructor other = (Constructor)obj;
  103.         if (getDeclaringClass() == other.getDeclaringClass()) {
  104.         /* Avoid unnecessary cloning */
  105.         Class[] params1 = parameterTypes;
  106.         Class[] params2 = other.parameterTypes;
  107.         if (params1.length == params2.length) {
  108.             for (int i = 0; i < params1.length; i++) {
  109.             if (params1[i] != params2[i])
  110.                 return false;
  111.             }
  112.             return true;
  113.         }
  114.         }
  115.     }
  116.     return false;
  117.     }
  118.  
  119.     /**
  120.      * Returns a hashcode for this Constructor. The hashcode is
  121.      * the same as the hashcode for the underlying constructor's
  122.      * declaring class name.
  123.      */
  124.     public int hashCode() {
  125.     return getDeclaringClass().getName().hashCode();
  126.     }
  127.  
  128.     /**
  129.      * Return a string describing this Constructor.  The string is
  130.      * formatted as the constructor access modifiers, if any,
  131.      * followed by the fully-qualified name of the declaring class,
  132.      * followed by a parenthesized, comma-separated list of the
  133.      * constructor's formal parameter types.  For example:
  134.      * <pre>
  135.      *    public java.util.Hashtable(int,float)
  136.      * </pre>
  137.      *
  138.      * <p>The only possible modifiers for constructors are the access
  139.      * modifiers <tt>public</tt>, <tt>protected</tt> or
  140.      * <tt>private</tt>.  Only one of these may appear, or none if the
  141.      * constructor has default (package) access.
  142.      */
  143.     public String toString() {
  144.     try {
  145.         StringBuffer sb = new StringBuffer();
  146.         int mod = getModifiers();
  147.         if (mod != 0) {
  148.         sb.append(Modifier.toString(mod) + " ");
  149.         }
  150.         sb.append(Field.getTypeName(getDeclaringClass()));
  151.         sb.append("(");
  152.         Class[] params = parameterTypes; // avoid clone
  153.         for (int j = 0; j < params.length; j++) {
  154.         sb.append(Field.getTypeName(params[j]));
  155.         if (j < (params.length - 1))
  156.             sb.append(",");
  157.         }
  158.         sb.append(")");
  159.         Class[] exceptions = exceptionTypes; // avoid clone
  160.         if (exceptions.length > 0) {
  161.         sb.append(" throws ");
  162.         for (int k = 0; k < exceptions.length; k++) {
  163.             sb.append(exceptions[k].getName());
  164.             if (k < (exceptions.length - 1))
  165.             sb.append(",");
  166.         }
  167.         }
  168.         return sb.toString();
  169.     } catch (Exception e) {
  170.         return "<" + e + ">";
  171.     }
  172.     }
  173.  
  174.     /**
  175.      * Uses the constructor represented by this Constructor object to
  176.      * create and initialize a new instance of the constructor's
  177.      * declaring class, with the specified initialization parameters.
  178.      * Individual parameters are automatically unwrapped to match
  179.      * primitive formal parameters, and both primitive and reference
  180.      * parameters are subject to widening conversions as necessary.
  181.      * Returns the newly created and initialized object.
  182.      *
  183.      * <p>Creation proceeds with the following steps, in order:
  184.      *
  185.      * <p>If the class that declares the underlying constructor
  186.      * represents an abstract class, the creation throws an
  187.      * InstantiationException.
  188.      *
  189.      * <p>If this Constructor object enforces Java language access
  190.      * control and the underlying constructor is inaccessible, the
  191.      * creation throws an IllegalAccessException.
  192.      *
  193.      * <p>If the number of actual parameters supplied via initargs is
  194.      * different from the number of formal parameters required by the
  195.      * underlying constructor, the creation throws an
  196.      * IllegalArgumentException.
  197.      *
  198.      * <p>A new instance of the constructor's declaring class is
  199.      * created, and its fields are initialized to their default
  200.      * initial values.
  201.      *
  202.      * <p>For each actual parameter in the supplied initargs array:
  203.      *
  204.      * <p>If the corresponding formal parameter has a primitive type,
  205.      * an unwrapping conversion is attempted to convert the object
  206.      * value to a value of the primitive type.  If this attempt fails,
  207.      * the creation throws an IllegalArgumentException.
  208.      *
  209.      * <p>If, after possible unwrapping, the parameter value cannot be
  210.      * converted to the corresponding formal parameter type by an
  211.      * identity or widening conversion, the creation throws an
  212.      * IllegalArgumentException.
  213.      *
  214.      * <p>Control transfers to the underlying constructor to
  215.      * initialize the new instance.  If the constructor completes
  216.      * abruptly by throwing an exception, the exception is placed in
  217.      * an InvocationTargetException and thrown in turn to the caller
  218.      * of newInstance.
  219.      *
  220.      * <p>If the constructor completes normally, returns the newly
  221.      * created and initialized instance.
  222.      *
  223.      * @exception IllegalAccessException    if the underlying constructor
  224.      *              is inaccessible.
  225.      * @exception IllegalArgumentException  if the number of actual and formal
  226.      *              parameters differ, or if an unwrapping conversion fails.
  227.      * @exception InstantiationException    if the class that declares the
  228.      *              underlying constructor represents an abstract class.
  229.      * @exception InvocationTargetException if the underlying constructor
  230.      *              throws an exception.
  231.      */
  232.     public native Object newInstance(Object[] initargs)
  233.     throws InstantiationException, IllegalAccessException,
  234.         IllegalArgumentException, InvocationTargetException;
  235. }
  236.